﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmLocationManager : Form
    {
        public frmLocationManager()
        {
            InitializeComponent();
            GetLocationList();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private List<int>GetSelectedLocations()
        {

            List<int> selectedLocations = new List<int>();
            int locationId;

            foreach (DataGridViewRow rw  in dgvLocationList.SelectedRows)
            {

                int.TryParse(rw.Cells[0].Value.ToString(), out locationId);
                selectedLocations.Add(locationId);
            }
       
            
            return selectedLocations;
        }

        private void DeleteLocation()
        {
            foreach (int i in GetSelectedLocations())
            {
                Storage st = new Storage();
                st.StoreId = i;
                StorageDataControl.Instance.Delete(st);
            }
        }

        private void EditLocation()
        {
            if (dgvLocationList.CurrentRow != null)
            {
                int Storeid;
                int.TryParse(dgvLocationList.CurrentRow.Cells[0].Value.ToString(), out Storeid);

                frmStorage StoreForm = new frmStorage(Storeid);
                StoreForm.UpdateStoreList += new frmStorage.UpdateStoreListHandler(GetLocationList);
                StoreForm.Show();
            }
            }
            
        private void AddLocation()
        {
            frmStorage StoreForm = new frmStorage();
            StoreForm.UpdateStoreList += new frmStorage.UpdateStoreListHandler(GetLocationList);
            StoreForm.Show();
        }

        private void GetLocationList()
        {
            IList<Storage> storeList = new List<Storage>();

            storeList = StorageDataControl.Instance.GetAllStores();

            // manual bind
            dgvLocationList.AutoGenerateColumns = false;


            dgvLocationList.DataSource = storeList;
            dgvLocationList.Columns[0].DataPropertyName = "StoreId";
            dgvLocationList.Columns[1].DataPropertyName = "StoreName";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            AddLocation();
        }

        private void dgvLocationList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditLocation();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult res;

            res = MessageBox.Show("Are you sure  you want to delete selected items(s)", "Inventory", MessageBoxButtons.YesNo);

            if (res == DialogResult.Yes)
            {
                DeleteLocation();
            }

            GetLocationList();
        }




    }
}
